﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDShop.Business;
using BDShop.Business.Cache;
using BDShop.Entities;
using BDShop.Filters;
using BDShop.Models;
using BDShop.Resources;
using BDShop.Utilities.Managers;
using PagedList;
using BDShop.Configuration;

namespace BDShop.Controllers
{   
    public class CategoryController : Controller
    {
        public ActionResult Index(int catID)
        {
            //Get category detail
            Category category = CategoryCache.GetByID(catID);
            CategoryModels categoryModel=new CategoryModels();
            categoryModel.ID = category.ID;
            categoryModel.Name = category.Name;

            return View(categoryModel);
        }

        public ActionResult ProductList(int catID,string catName, int sortOrder = 0, int? page = 1,string price="All", string currentFilter = "")
        {
            int pageSize = BDShopConfiguration.Web.pageSize;

            ViewBag.Url= UrlManager.ProductOfCatUrl(catID,catName);
            //Get value for sort ddl
            List<SelectListItem> lstSort=new List<SelectListItem>();
            SelectListItem itemDefault = new SelectListItem() { Text = FO.macdinh, Value = "0", Selected = (0 == sortOrder) ? true : false };
            SelectListItem itemNameAZ = new SelectListItem() { Text = FO.tensanphamaz, Value = "1", Selected = (1 == sortOrder) ? true : false };
            SelectListItem itemNameZA = new SelectListItem() { Text = FO.tensanphamza, Value = "2", Selected = (2 == sortOrder) ? true : false };
            SelectListItem itemPriceTC = new SelectListItem() { Text = FO.giathapcao, Value = "3", Selected = (3 == sortOrder) ? true : false };
            SelectListItem itemPriceCT = new SelectListItem() { Text = FO.giacaothap, Value = "4", Selected = (4 == sortOrder) ? true : false };
            SelectListItem itemRateH = new SelectListItem() { Text = FO.binhchoncaonhat, Value = "5", Selected = (5 == sortOrder) ? true : false };
            SelectListItem itemRateL = new SelectListItem() { Text = FO.binhchonthapnhat, Value = "6", Selected = (6 == sortOrder) ? true : false };
            IEnumerable<SelectListItem> eItem = new SelectListItem[]{itemDefault,itemNameAZ,itemNameZA,itemPriceTC,itemPriceCT,itemRateH,itemRateL};
            lstSort.AddRange(eItem);
            ViewBag.Sort = lstSort;

            ViewBag.page = page;
            ViewBag.sortOrder = sortOrder;
            ViewBag.Price = price;
           
            List<ProductCacheComplex> lstProduct=new List<ProductCacheComplex>();
            //get product of category
            List<ProductCategory> lstProductOfCat = ProductCategoryCache.Instance.Get().Where(pc => pc.CategoryID == catID).ToList();
            //Get Product Of Category 
            lstProduct = lstProductOfCat.Join(ProductCache.Instance.Get(), pc => pc.ProductID, p => p.ID, (pc, p) => p).Distinct().ToList();

            switch (sortOrder)
            {
                case 1:
                    lstProduct = lstProduct.OrderBy(p => p.Name).ToList();
                    break;
                case 2:
                    lstProduct = lstProduct.OrderByDescending(p => p.Name).ToList();
                    break;
                case 3:
                    lstProduct = lstProduct.OrderBy(p => p.Price).ToList();
                    break;
                case 4:
                    lstProduct = lstProduct.OrderByDescending(p => p.Price).ToList();
                    break;
                case 5:
                    lstProduct = lstProduct.OrderByDescending(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                case 6:
                    lstProduct = lstProduct.OrderBy(p => Math.Ceiling((float)p.Rating / p.Rater)).ToList();
                    break;
                default:
                    lstProduct = lstProduct.OrderByDescending(p => p.DateCreated).ToList();
                    break;
            }

            //Filter by price
            List<ProductCacheComplex> lstProductResult=new List<ProductCacheComplex>();
            if(price=="All")
            {
                lstProductResult = lstProduct;
            }
            else
            {
                string[] lstPrice = price.Split('-');
                foreach (var s in lstPrice)
                {
                    switch (s)
                    {
                        case "0":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price < 100000)).ToList();
                            break;
                        case "1":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 100000 && p.Price <= 500000)).ToList();
                            break;
                        case "2":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 500000 && p.Price <= 1000000)).ToList();
                            break;
                        case "3":
                            lstProductResult = lstProductResult.Union(lstProduct.Where(p => p.Price > 1000000)).ToList();
                            break;
                    }
                }
            }

            //Paging
            int pageNumber = (page ?? 1);

            return PartialView("Partial/_ProductList", lstProductResult.ToPagedList(pageNumber, pageSize));
        }

        public ActionResult LeftMenu(int catID)
        {
            ViewBag.CatID = catID;
            //load category left menu
            List<Category> parentCat = CategoryCache.GetParent(0);
           
            return PartialView("Partial/_LeftCategory", parentCat);
        }
    }
}
